/*
 * UrlNodeUserObject.java
 * 
 * Copyright (c) 2011 Administrator. 
 * 
 * This file is part of RMS.
 * 
 * RMS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * RMS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with RMS.  If not, see <http ://www.gnu.org/licenses/>.
 */
package com.itrustsolution.rms.util;

import com.icesoft.faces.component.tree.IceUserObject;
import com.itrustsolution.rms.managedbean.PanelStackBean;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.swing.tree.DefaultMutableTreeNode;

/**
 * The UrlNodeUserObject object is responsible for storing extra data
 * for a url.  The url along with text is bound to a ice:commanLink object which
 * will launch a new browser window pointed to the url.
 * @author Administrator
 */
public class UrlNodeUserObject extends IceUserObject {

    // url to show when a node is clicked
    private String url;

    private boolean selected;

    // displayPanel to show when a node is clicked
    private String displayPanel;

    // panel stack which will be manipulated when a command links action is fired.
    private PanelStackBean panelStack;

    /**
     * ActionListener method called when a node in the tree is clicked.  Sets
     * the selected panel of the reference panelStack to the value of the instance
     * variable #displayPanel.   
     *
     * @param action JSF action event.
     */
    public void selectPanelStackPanel(ActionEvent action){
        if (panelStack != null){
            panelStack.setSelectedPanel(displayPanel);
        }
    }

    public UrlNodeUserObject(DefaultMutableTreeNode wrapper) {
        super(wrapper);
        
        // get a reference to the PanelStackBean from the faces context
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Object panelStackObject =
                facesContext.getApplication()
                        .createValueBinding("#{panelStack}")
                        .getValue(facesContext);
        if (panelStackObject instanceof PanelStackBean){
            panelStack = (PanelStackBean)panelStackObject;
        }
    }

    /**
     * Gets the name of a panel in the panel stack which is associated with
     * this object.
     *
     * @return name of a panel in the panel stack
     */
    public String getDisplayPanel() {
        return displayPanel;
    }

    /**
     * Sets the name of a panelStack panel, assumed to be valid.
     *
     * @param displayPanel panelStack panel name.
     */
    public void setDisplayPanel(String displayPanel) {
        this.displayPanel = displayPanel;
    }

    /**
     * Gets the url value of this IceUserObject.
     *
     * @return string representing a URL.
     */
    public String getUrl() {
        return url;
    }

    /**
     * Sets the URL.
     *
     * @param url a valid URL with protocol information such as
     *            http://icesoft.com
     */
    public void setUrl(String url) {
        this.url = url;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
        System.out.println("Setting value " + selected);
    }

}
